package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.dto.NursingProjectDto;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.service.INursingProjectService;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 护理项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-26
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper,NursingProject> implements INursingProjectService
{

    private static final  String CACHE_KEY_PREFIX = "nursingProject:all";
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return this.getById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return getBaseMapper().selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        //先更新在再删除Redis中的数据
        boolean flag = this.save(nursingProject);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag? 1:0;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        boolean flag = this.updateById(nursingProject);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag?1:0;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        boolean flag = this.removeByIds(Arrays.asList(ids));
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag?1:0;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        boolean flag = this.removeById(id);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag?1:0;
    }

    /**
     * 获取全部护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> getAll() {
        List<NursingProjectVo> nursingProjectVoList = (List<NursingProjectVo>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if(!CollectionUtils.isEmpty(nursingProjectVoList)){
            //部位用就直接返回
            System.out.println("从缓存中获取数据");
            return  nursingProjectVoList;
        }
        System.out.println("从数据库中获取数据");


        nursingProjectVoList = getBaseMapper().getAll();
        //设置值到Redis中
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX,nursingProjectVoList);

        return nursingProjectVoList;
    }

    @Override
    public TableDataInfo<NursingProject> memberOrderList(NursingProjectDto nursingProjectDto) {
        PageHelper.startPage(nursingProjectDto.getPageNum(),nursingProjectDto.getPageSize());
        List<NursingProject> nursingProjectList = nursingProjectMapper.memberOrderList(nursingProjectDto);
        Page<NursingProject> page =(Page<NursingProject>)  nursingProjectList;


        TableDataInfo<NursingProject> nursingProjectTableDataInfo = new TableDataInfo<>();
        nursingProjectTableDataInfo.setTotal(page.getTotal());
        nursingProjectTableDataInfo.setRows(page.getResult());
        return nursingProjectTableDataInfo;
    }
}
